package io.adrainty.bolee.security.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.basic.TreeDTO;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.security.models.entity.MenuEntity;
import io.adrainty.bolee.security.models.entity.ResourceEntity;
import io.adrainty.bolee.security.service.IResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/10 下午3:26
 */

@Tag(name = "资源管理")
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private IResourceService resourceService;

    @PostMapping("/menus/{systemCode}")
    @Operation(summary = "获取菜单")
    public ResultDTO<List<MenuEntity>> getMenus(@PathVariable("systemCode") String systemCode) {
        List<MenuEntity> menuEntities = resourceService.getMenus(systemCode);
        return ResultDTOUtil.success(menuEntities);
    }

    @PostMapping("/tree")
    @Operation(summary = "获取资源树")
    public ResultDTO<TreeDTO> getResourceTree(@RequestBody ResourceEntity resourceEntity) {
        TreeDTO resourceEntities = resourceService.getResourceTree(resourceEntity);
        return ResultDTOUtil.success(resourceEntities);
    }

    @PostMapping("/page/{pageNum}/{pageSize}")
    @Operation(summary = "获取资源分页")
    public ResultDTO<IPage<ResourceEntity>> getResourcePage(@PathVariable("pageNum") Integer pageNum,
                                                            @PathVariable("pageSize") Integer pageSize,
                                                            @RequestBody ResourceEntity resourceEntity) {
        IPage<ResourceEntity> resourceEntities = resourceService.getResourcePage(pageNum, pageSize, resourceEntity);
        return ResultDTOUtil.success(resourceEntities);
    }

    @PutMapping
    @Operation(summary = "资源添加")
    public ResultDTO<ResourceEntity> createResource(@RequestBody ResourceEntity resourceEntity) {
        ResourceEntity result = resourceService.createResource(resourceEntity);
        return ResultDTOUtil.success(result);
    }

    @PatchMapping
    @Operation(summary = "资源修改")
    public ResultDTO<Boolean> updateResource(@RequestBody ResourceEntity resourceEntity) {
        Boolean result = resourceService.updateResource(resourceEntity);
        return ResultDTOUtil.success(result);
    }

    @PostMapping("/list")
    @Operation(summary = "资源列表")
    public ResultDTO<List<ResourceEntity>> findResourceList(@RequestBody ResourceEntity resourceEntity) {
        List<ResourceEntity> result = resourceService.findResourceList(resourceEntity);
        return ResultDTOUtil.success(result);
    }


}
